package com.dc.ftp.activity.loan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.dc.ftp.R;
import com.dc.ftp.adapter.SelectPlatformLvAdapter;
import com.dc.ftp.base.SPBaseActivity;

public class CollectLoanInfoActivity extends SPBaseActivity implements View.OnClickListener{

    private AMapLocationClient mLocationClient;
    private AMapLocationClientOption mLocationOption;
    private TextView tvCity;
    private RelativeLayout rlyRealName;
    private TextView tvPlatform;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect_loan_info);
        super.init();
        setHeadTitle(true,"认证资料");


    }

    @Override
    public void initView() {
        findViewById(R.id.rly_real_name_info).setOnClickListener(this);
        findViewById(R.id.rly_personal_info).setOnClickListener(this);
        findViewById(R.id.rly_connect_person_info).setOnClickListener(this);
        findViewById(R.id.rly_work_info).setOnClickListener(this);
        findViewById(R.id.rly_city_info).setOnClickListener(this);
        findViewById(R.id.btn_next).setOnClickListener(this);
        findViewById(R.id.rly_choose_platform).setOnClickListener(this);

        tvCity = (TextView) findViewById(R.id.tv_city_info);
        tvCity.setTextColor(getResources().getColor(R.color.title_black));

        tvPlatform = (TextView) findViewById(R.id.tv_platform_name);

    }

    @Override
    public void initData() {
        //初始化高德地图并启动一次定位
        initAmap();
    }

    @Override
    public void initEvent() {

    }


    private void initAmap() {
        //声明定位回调监听器

        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        /**
         * 设置定位场景，目前支持三种场景（签到、出行、运动，默认无场景）
         */
        //mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
        if(null != mLocationClient){
            mLocationClient.setLocationOption(mLocationOption);
            //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
            mLocationClient.stopLocation();
            mLocationClient.startLocation();
        }
        /*//设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
          mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);*/
        //设置定位模式为AMapLocationMode.Battery_Saving，低功耗模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);

        //获取一次定位结果：
        //该方法默认为false。
        mLocationOption.setOnceLocation(true);

        //获取最近3s内精度最高的一次定位结果：
        //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。
        // 如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
        mLocationOption.setOnceLocationLatest(true);

        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);

        //设置是否允许模拟位置,默认为true，允许模拟位置
        mLocationOption.setMockEnable(false);

        //单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
        mLocationOption.setHttpTimeOut(20000);

        //关闭定位缓存机制
        mLocationOption.setLocationCacheEnable(false);

        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();

    }


    public AMapLocationListener mLocationListener = new AMapLocationListener() {

        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation != null) {
                if (amapLocation.getErrorCode() == 0) {
                    //可在其中解析amapLocation获取相应内容。

                    amapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
                    amapLocation.getLatitude();//获取纬度
                    amapLocation.getLongitude();//获取经度
                    amapLocation.getAccuracy();//获取精度信息
                    amapLocation.getAddress();//地址，如果option中设置isNeedAddress为false，则没有此结果，网络定位结果中会有地址信息，GPS定位不返回地址信息。
                    amapLocation.getCountry();//国家信息
                    String province = amapLocation.getProvince();//省信息
                    String city = amapLocation.getCity();//城市信息
                    amapLocation.getDistrict();//城区信息
                    amapLocation.getStreet();//街道信息
                    amapLocation.getStreetNum();//街道门牌号信息
                    amapLocation.getCityCode();//城市编码
                    amapLocation.getAdCode();//地区编码

                    //showToast(province + city);

                    tvCity.setText(province + "-"+city);
//获取定位时间---

                } else {
                    tvCity.setText("定位失败,点击重试");

                    //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                 /*   showToast("定位失败"+"location Error, ErrCode:"
                            + amapLocation.getErrorCode() + ", errInfo:"
                            + amapLocation.getErrorInfo());*/

                    Log.e("AmapError", "location Error, ErrCode:"
                            + amapLocation.getErrorCode() + ", errInfo:"
                            + amapLocation.getErrorInfo());
                }
            }
        }



    };

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.rly_real_name_info:
                startActivity(new Intent(this,RealNameInfoActivity.class));
                break;
            case R.id.rly_personal_info:
                startActivity(new Intent(this,PersonalInfoActivity.class));
                break;
            case R.id.rly_connect_person_info:
                startActivity(new Intent(this,ConnectPersonInfoActivity.class));
                break;
            case R.id.rly_work_info:
                startActivity(new Intent(this,WorkInfoActivity.class));
                break;
            case R.id.rly_city_info:
                tvCity.setText("定位中...");
                //启动定位
                mLocationClient.startLocation();
                break;
            case R.id.rly_choose_platform:
                startActivityForResult(new Intent(this,SelectLoanPlatformActivity.class),222);
                break;
            case R.id.btn_next:
                break;
            default:break;
        }


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode== Activity.RESULT_CANCELED){return;}

        if (requestCode==222&&resultCode==333){
            tvPlatform.setText(data.getStringExtra("plat_name"));
        }
    }
}
